package com.nebula.salary.portal.controller.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.nebula.salary.common.constants.Const;
import com.nebula.salary.common.constants.ReturnMessage;
import com.nebula.salary.common.exception.ErrorException;
import com.nebula.salary.common.result.ServerResponse;
import com.nebula.salary.common.utils.SecurityUtil;
import com.nebula.salary.model.dto.PageDto;
import com.nebula.salary.model.pojo.Question;
import com.nebula.salary.model.vo.PageResult;
import com.nebula.salary.portal.service.IQuestionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 密保模板表 前端控制器
 * </p>
 *
 * @author codex
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/manage/security")
@Api(tags = "密保问题")
public class QuestionController {
	
	@Resource
	private IQuestionService questionService;

	@ApiOperation("删除密保模板")
	@DeleteMapping("/{ids}")
	@Transactional(rollbackFor = Exception.class)
	public ServerResponse<String> deleteQuestion(@ApiParam(value = "密保模板编号", required = true) @PathVariable Integer[] ids) {
		try {
			questionService.removeByIds(Arrays.asList(ids));
			return ServerResponse.createBySuccessMessage(ReturnMessage.DELETE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return ServerResponse.createByErrorMessage(ReturnMessage.DELETE_ERROR);
		}
	}

	@ApiOperation("修改密保模板")
	@PutMapping("/")
	@Transactional(rollbackFor = Exception.class)
	public ServerResponse<String> updateQuestion(@RequestBody Question question) {
		// 校验数据
		questionService.checkData(question);

		// 验证密保问题是否唯一
		if (Const.NOT_UNIQUE.equals(questionService.checkQuestionUnique(question))) {
			return ServerResponse.createByErrorMessage("修改安全问题'" + question.getQuestion() + "'失败，安全问题已存在");
		}

		question.setUserId(SecurityUtil.getUserId());
		return questionService.updateById(question)
				? ServerResponse.createBySuccessMessage(ReturnMessage.UPDATE_SUCCESS)
				: ServerResponse.createByErrorMessage(ReturnMessage.UPDATE_ERROR);
	}

	@ApiOperation("新增密保模板")
	@PostMapping("/")
	@Transactional(rollbackFor = Exception.class)
	public ServerResponse<String> addQuestion(@RequestBody Question question) {
		// 校验数据
		questionService.checkData(question);

		// 验证密保问题是否唯一
		if (Const.NOT_UNIQUE.equals(questionService.checkQuestionUnique(question))) {
			return ServerResponse.createByErrorMessage("新增安全问题'" + question.getQuestion() + "'失败，安全问题已存在");
		}

		question.setUserId(SecurityUtil.getUserId());
		return questionService.save(question) ? ServerResponse.createBySuccessMessage(ReturnMessage.INSERT_SUCCESS)
				: ServerResponse.createByErrorMessage(ReturnMessage.INSERT_ERROR);
	}

	@ApiOperation("根据编号获取密保模板")
	@GetMapping("/{id}")
	public ServerResponse<Map<String, Object>> getById(@ApiParam(value = "密保模板编号", required = true) @PathVariable Integer id) {
		
		Question question = questionService.getById(id);
		if (question == null) {
			throw new ErrorException("密保模板不存在");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("questionId", question.getQuestionId());
		map.put("question", question.getQuestion());
		return ServerResponse.createBySuccess(map);
	}

	@ApiOperation("获取密保模板")
	@GetMapping("/")
	public ServerResponse<PageResult> getListByPage(PageDto pageDto) {
		IPage<Question> pages = questionService.page(new Page<Question>(pageDto.getPage(), pageDto.getLimit()));
		List<Question> list = pages.getRecords();

		PageResult pageResult = new PageResult(pages.getTotal(), list.size(), list);

		return ServerResponse.createBySuccess(pageResult);
	}

}
